﻿using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;
using static Mekiasm.MekCraftingRecipe;

namespace Mekiasm
{
    public class SolarPanel : ComplexModelItem
    {
        public static int Index;
        public SolarPanel() : base(new ModelItem[] { new ModelItem("con", "ports"), new ModelItem("bottom", "ports"), new ModelItem("plate", "solarpanel") }, "TYN", "基础太阳能板")
        {
            
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {

            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, null,
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig()
            {
                MaxPower = 10240,
                InputPowerLimit = 32,
                OutputPowerLimit = 32
            });
            ElectricSystem.Add(new SolarPanelElement(x, y, z));
        }
        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new SolarPanelElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ElectricSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentSolarPanel mcomponent = ILibrary.SubsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentSolarPanel>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new SolarPanelWidget(mcomponent, componentMiner);
            return true;
        }
        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value)
        {
            return true;
        }
    }
}
